#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cctype>
#include <cstdlib>
#include <cstring>
#include <cassert>
#include <algorithm>
#include <functional>
#include <utility>
#include <vector>
#include <string>
#include <map>
#include <set>
#include <deque>

using namespace std;

#define sz(a) (int)(a.size())
#define fill(a,x) memset(a,x,sizeof(x))
#define mp make_pair
#define pb push_back

typedef long long ll;
typedef unsigned long long ull;

const int MAXN = 501;
const int INF = 0x3f3f3f3f;
const ll LINF = 0x3f3f3f3f3f3f3f3fLL;

struct edge {
	int a, b, cap, flow;
};

int n, s, t, m, d[MAXN], ptr[MAXN], q[MAXN];
vector < edge > e;
vector < int > g[MAXN];
int net;

int add_edge(int a, int b, int cap) {
	edge e1 = {a, b, cap, 0},
		 e2 = {b, a, 0, 0};
	g[a].pb(sz(e)), e.pb(e1);
	g[b].pb(sz(e)), e.pb(e2);
	return 0;
}

bool bfs() {
	int qh = 0, qt = 0;
	q[qt++] = s;
	memset(d, -1, (n + 1) * sizeof(d[0]));
	d[s] = 0;
	while (qh < qt && d[t] == -1) {
		int v = q[qh++];
		for (int i = 0; i < sz(g[v]); ++i) {
			int id = g[v][i],
				to = e[id].b;
			if (d[to] == -1 && e[id].flow + net < e[id].cap) {
				q[qt++] = to;
				d[to] = d[v] + 1;
			}
		}
	}
	return d[t] != -1;
}

int dfs(int v, int flow) {
	if (!flow) return 0;
	if (v == t) return flow;
	for (; ptr[v] < sz(g[v]); ++ptr[v]) {
		int id = g[v][ptr[v]],
			to = e[id].b;
		if (d[to] != d[v] + 1) continue;
		int pushed = dfs(to, min(flow, e[id].cap - e[id].flow));
		if (pushed) {
			e[id].flow += pushed;
			e[id ^ 1].flow -= pushed;
			return pushed;
		}
	}
	return 0;
}

ll dinic() {
	ll flow = 0LL;
	net = 1000000000;
	do {
		net >>= 1;
		for (;;) {
			if (!bfs()) break;
			memset(ptr, 0, (n + 1) * sizeof(ptr[0]));
			while (int pushed = dfs(s, INF))
				flow += pushed;
		}
	} while (net);
	return flow;
}

int main() {
	cin >> n >> m;
	for (int i = 1; i <= n; ++i)
		g[i].clear();
	e.clear();
	for (int i = 0; i < m; ++i) {
		int a, b, c;
		cin >> a >> b >> c;
		add_edge(a, b, c);
	}
	//cin >> s >> t;
	s = 1, t = n;
	cout << dinic() << endl;
	return 0;
}
